package ar.edu.itba.tp1.gps.heuristics;

import ar.edu.itba.tp1.gps.GPSRun;
import ar.edu.itba.tp1.gps.api.GPSProblemImpl;
import ar.edu.itba.tp1.gps.api.GPSState;
import ar.edu.itba.tp1.gps.board.ArithmeticsRestrictions;

public class GPSProblemSecondHeuristic extends GPSProblemImpl {
	public GPSProblemSecondHeuristic(ArithmeticsRestrictions restrictions) {
		super(restrictions);
	}
	
	@Override
	public Integer getHValue(GPSState state) {
		int rowsAndColumnsCompleted = 0;
		for (int i = 0; i < state.getValues()[0].length; i++) {
			if (isRowNCompleted(state, i)) {
				rowsAndColumnsCompleted++;
			}
		}
		for (int i = 0; i < state.getValues()[0].length; i++) {
			if (isColumnNCompleted(state, i)) {
				rowsAndColumnsCompleted++;
			}
		}
		return GPSRun.n*2 - rowsAndColumnsCompleted;
	}

	private boolean isColumnNCompleted(GPSState state, int n) {
		Integer[][] matrix = state.getValues();
		for (int k = 0; k < matrix[n].length; k++) {
			if (matrix[k][n] == null) {
				return false;
			}
		}
		return true;
	}

	private boolean isRowNCompleted(GPSState state, int n) {
		Integer[][] matrix = state.getValues();
		for (int k = 0; k < matrix[n].length; k++) {
			if (matrix[n][k] == null) {
				return false;
			}
		}
		return true;
	}
	
}
